package com.lc.user.express.ordering;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.CityAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetCityId;
import com.lc.user.express.httpserver.GetCityList;
import com.lc.user.express.model.CityModel;
import com.lc.user.express.my.MyStardandActivity;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/2. 选择城市
 */
public class SelectCityActivity extends BaseSecondActivity {
    public static OnCityLocation onCityLocation = null;
    private List<CityModel> list = new ArrayList<>();//城市列表数据
    private CityAdapter adapter;//城市列表adapter
    private ListView lv;//城市列表
    private TextView tv_location_city;//当前定位城市

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_city);
        setTitle("选择城市");
        setLeftButtonBgResId2(R.mipmap.cha, 60, 60);
        initView();
        getData();

    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);

        View v = LayoutInflater.from(cnt).inflate(R.layout.header_select_city_list, null);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) v);
        tv_location_city = (TextView) v.findViewById(R.id.tv_city);
        tv_location_city.setText(TextUtils.isEmpty(AppContext.myPreferences.getCityLocation()) ? "定位中..." : AppContext.myPreferences.getCityLocation());
        lv.addHeaderView(v);
        adapter = new CityAdapter(cnt, list);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                OnNotifyCityChanged(list.get(position - 1).getCity(), list.get(position - 1).getId());

            }
        });
        tv_location_city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!"定位中...".equals(tv_location_city.getText().toString())) {
                    getCityId(tv_location_city.getText().toString());
                }

            }
        });

        onCityLocation = new OnCityLocation() {
            @Override
            public void onCity(String city) {
                tv_location_city.setText(city);
            }
        };


    }

    private void OnNotifyCityChanged(String city, String cityId) {
        if (MyStardandActivity.onCityChanged != null) {
            MyStardandActivity.onCityChanged.onCityChanged(city, cityId);
            finish();
            return;
        }
        if (OrderingFragment.onCityChanged != null) {
            OrderingFragment.onCityChanged.onCityChanged(city, cityId);
            finish();
        }

    }

    private void getData() {
        new GetCityList(new AsyCallBack<GetCityList.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetCityList.Info info) throws Exception {
                list.addAll(info.dataList);
//                CityModel cityModel = new CityModel();
//                cityModel.setCity("北京市");
//                cityModel.setId("111");
//                list.add(cityModel);
//                CityModel cityModel1 = new CityModel();
//                cityModel1.setCity("上海市");
//                cityModel1.setId("112");
//                list.add(cityModel1);
//                CityModel cityModel2 = new CityModel();
//                cityModel2.setCity("广州市");
//                cityModel2.setId("113");
//                list.add(cityModel2);
                adapter.updateListView(list);
            }
        }).execute(cnt);
    }

    private void getCityId(final String city) {
        new GetCityId(city, new AsyCallBack<GetCityId.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetCityId.Info info) throws Exception {

                AppContext.myPreferences.setCityId(info.cityid);
                OnNotifyCityChanged(AppContext.myPreferences.getCityLocation(), AppContext.myPreferences.getCityId());
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

            }
        }).execute(cnt);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onCityLocation = null;
    }

    public interface OnCityLocation {
        void onCity(String city);
    }
}
